﻿/*
 * Created by SharpDevelop.
 * User: tfuru
 * Date: 2008/01/11
 * Time: 20:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using Process = nicosb.Lib.Process;
using System.Net;
using System.Text;
using Timer = System.Windows.Forms.Timer;

namespace nicosb
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private static string CONFIG_FILENAME = "nicosb.xml";
		private static Config m_conf = null;
		private Process m_ncron_process = null;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			init();
		}

		void MainFormLoad(object sender, EventArgs e)
		{
			
		}
		
		private void init(){
			Trace.WriteLine("init()");
			//設定ファイル読み込み
			m_conf = new Config();
			if(!File.Exists(CONFIG_FILENAME)){
				//設定ファイルが無かったので初期値を設定
				m_conf.setCronPhpPath("C:\\work\\php\\nicosb\\server\\bin\\cron.php");
				m_conf.setTaskMinutes("5");
				m_conf.setApachePort("80");
				m_conf.setApacheDocumentRoot("");
				m_conf.setRegisterUrls("http://nicosb.orz.hm/api/register.php");
				
				//保存
				Config.save(m_conf,CONFIG_FILENAME);
			}
			else{
				//あった場合
				m_conf = Config.load(CONFIG_FILENAME);
			}
			
			//起動時に１回 cron.phpを 起動
			cron_run();
			
			//分をミリ秒に
			int interval = Int32.Parse(m_conf.getTaskMinutes())*60000;
			
			Timer timer = new Timer();
			timer.Tick += new EventHandler(eventHandler);
			timer.Interval  = interval;
			timer.Start();

			//Apache の起動状態をチェック
			
			//起動通知送信
			
		}
		
		public void eventHandler(object sender, EventArgs e) {
			Trace.WriteLine(DateTime.Now);
			//cron.phpを 起動
			cron_run();
		}
		
		//cron.phpを 起動
		public void cron_run(){
			if((!(m_conf.getCronPhpPath().Equals(""))) & (File.Exists(m_conf.getCronPhpPath()))){
				//cron.php
				String arguments = "\"php "+m_conf.getCronPhpPath()+"\"";
				Trace.WriteLine(arguments);
				if(m_ncron_process == null){
					//初回だけ引数付き
					m_ncron_process = new Process(arguments);
				}
				else{
					m_ncron_process.setArguments(arguments);
				}
				
				m_ncron_process.start(false);
				Trace.WriteLine(m_ncron_process.getResults());
			}
		}
		
		void ToolStripMenuItem1Click(object sender, EventArgs e)
		{
			//終了
			//タスクトレイのアイコン削除
			this.notifyIcon1.Visible = false;
			Application.Exit();
		}
		
		//URLにアクセスする
		private void webRequest(string url){
			try{
				WebRequest req = WebRequest.Create(url);
				WebResponse res = req.GetResponse();
			}catch(Exception e){
				Console.WriteLine("{0} Exception caught.", e);
			}
		}
	}
}
